#include "general.h"

extern int G_W, G_H;
extern QPoint shuttle_pos;
extern HUD *global_hud;
extern QVector<int> *vect_exp_count;

GENERAL::GENERAL():QObject()
{
    load_num_explosions();
    init();

}

void GENERAL::Start()
{

    view->setFixedSize(G_W, G_H);
//    view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers | QGL::DirectRendering)));
//    view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    view->showFullScreen();

}

void GENERAL::init() //главная функция инициализации
{

    cash = new CASH(0,QCoreApplication::applicationDirPath () +"/Res/");

    sound = new SOUND_POWER(cash,"Res/Sound/Levels/Level_1.ogg",this);

    shuttle_life=true;

//разгоняем рандометр
    qsrand(QTime::currentTime().msec() );

//настройка сцены
    scene = new SCENE(this);
    scene->setSceneRect(0, 0, G_W, G_H);
    scene->setBackgroundBrush(QBrush(Qt::black));

//настройка вьювера
    view = new QGraphicsView(scene);
    view->setSceneRect(scene->sceneRect());

    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

// time lines
    tl_anim_tiles = new QTimeLine(1000, this);

    tl_anim_tiles->setEasingCurve(QEasingCurve::Linear);
    tl_anim_tiles->setLoopCount(0);
    tl_anim_tiles->setFrameRange(0, 1);

//tl shuttle bullets
    tl_shuttle_bullets = new QTimeLine(1000, this);

    tl_shuttle_bullets->setEasingCurve(QEasingCurve::Linear);
    tl_shuttle_bullets->setLoopCount(0);
    tl_shuttle_bullets->setFrameRange(0, 1);

    tl_shuttle_bullets->start();

//tl gatlings
    tl_shuttle_gatling_spawn = new QTimeLine(200, this);

    tl_shuttle_gatling_spawn->setEasingCurve(QEasingCurve::Linear);
    tl_shuttle_gatling_spawn->setLoopCount(1);

//tl_cannons
    tl_shuttle_cannon_spawn = new QTimeLine(2000, this);

    tl_shuttle_cannon_spawn->setEasingCurve(QEasingCurve::Linear);
    tl_shuttle_cannon_spawn->setLoopCount(1);

    shuttle_cannon_activated = false;

//global timer
    global_tl = new QTimeLine(1000, this);
    global_tl->setLoopCount(0);
    global_tl->start();

//menu
    menu = new MAIN_MENU(cash);
    scene->addItem(menu);
    menu->setZValue(20);

    scene->setFocusItem(menu);
    menu->show();


//hud
    hud = new HUD(cash);
    global_hud = hud;

    scene->addItem(hud);
    hud->setZValue(10);

    QTimer::singleShot(10, hud, SLOT(hide_hud()));
    QTimer::singleShot(1, hud, SLOT(slot_unvis_hud()));

//настройка корабля
    shuttle = new Shuttle(scene,global_tl,cash, hud);

    scene->addItem(shuttle);
    shuttle->setZValue(0.5);
    shuttle->setPos((G_W/2) - (shuttle->boundingRect().width()/2), G_H - shuttle->boundingRect().height() - 50 + G_H);

    GAME_OVER_TEXT *game_over = new GAME_OVER_TEXT(this->cash);
    this->scene->addItem(game_over);

    connect(shuttle, SIGNAL(Uuuups_I_die()), game_over, SLOT(anim_show()));

//tiles
    bg_tile = new TILES(scene->sceneRect(), TERRAIN, "city", 2, false,cash);
    bg_tile_cover = new TILES(scene->sceneRect(), TERRAIN, "city", 2, true,cash);

    bg_tile->set_time_line(tl_anim_tiles);
    bg_tile_cover->set_time_line(tl_anim_tiles);

    scene->addItem(bg_tile);
    scene->addItem(bg_tile_cover);

    bg_tile->set_cover(bg_tile_cover);
    bg_tile_cover->set_cover(bg_tile);

//edges
    bg_edge = new TILES(scene->sceneRect(), EDGES, "city", 0, false,cash);
    bg_edge_cover = new TILES(scene->sceneRect(), EDGES, "city", 0, true,cash);

    bg_edge->set_time_line(tl_anim_tiles);
    bg_edge_cover->set_time_line(tl_anim_tiles);

    scene->addItem(bg_edge);
    scene->addItem(bg_edge_cover);

    bg_edge->set_cover(bg_edge_cover);
    bg_edge_cover->set_cover(bg_edge);

//clowds 1
    dec_clowds_1 = new DECORATIONS(scene->sceneRect(), CLOWDS, "earth", 2, 5, false,cash);
    dec_clowds_1_cover = new DECORATIONS(scene->sceneRect(), CLOWDS, "earth", 2, 5, true,cash);

    dec_clowds_1->set_time_line(tl_anim_tiles);
    dec_clowds_1_cover->set_time_line(tl_anim_tiles);

    scene->addItem(dec_clowds_1);
    scene->addItem(dec_clowds_1_cover);

    dec_clowds_1->set_cover(dec_clowds_1_cover);
    dec_clowds_1_cover->set_cover(dec_clowds_1);

    dec_clowds_1->setZValue(1);
    dec_clowds_1_cover->setZValue(1);

//clowds 2
    dec_clowds_2 = new DECORATIONS(scene->sceneRect(), CLOWDS, "earth", 2, 4, false,cash);
    dec_clowds_2_cover = new DECORATIONS(scene->sceneRect(), CLOWDS, "earth", 2, 4, true,cash);

    dec_clowds_2->set_time_line(tl_anim_tiles);
    dec_clowds_2_cover->set_time_line(tl_anim_tiles);

    scene->addItem(dec_clowds_2);
    scene->addItem(dec_clowds_2_cover);

    dec_clowds_2->set_cover(dec_clowds_2_cover);
    dec_clowds_2_cover->set_cover(dec_clowds_2);

    dec_clowds_2->set_basic_anim_speed(4);
    dec_clowds_2_cover->set_basic_anim_speed(4);

    dec_clowds_1->setZValue(1);
    dec_clowds_1_cover->setZValue(1);

//clowds 3
    dec_clowds_3 = new DECORATIONS(scene->sceneRect(), CLOWDS, "earth", 2, 3, false,cash);
    dec_clowds_3_cover = new DECORATIONS(scene->sceneRect(), CLOWDS, "earth", 2, 3, true,cash);

    dec_clowds_3->set_time_line(tl_anim_tiles);
    dec_clowds_3_cover->set_time_line(tl_anim_tiles);

    scene->addItem(dec_clowds_3);
    scene->addItem(dec_clowds_3_cover);

    dec_clowds_3->set_cover(dec_clowds_3_cover);
    dec_clowds_3_cover->set_cover(dec_clowds_3);

    dec_clowds_3->set_basic_anim_speed(8);
    dec_clowds_3_cover->set_basic_anim_speed(8);

    dec_clowds_1->setZValue(0.2);
    dec_clowds_1_cover->setZValue(0.2);

    tl_anim_tiles->start();

//text
    stage_text = new STAGE_TEXT(1, 1,cash);
    scene->addItem(stage_text);

    to_be_continued_text = new TO_BE_CONTINUED_TEXT(cash);
    scene->addItem(to_be_continued_text);

//соединения всех сигналлов и слотов класса
    connect(shuttle, SIGNAL(signal_keys_pressed()), this, SLOT(slot_keys_pressed()));

//clowds 1
    connect(shuttle, SIGNAL(signal_fly(MOVE_DIRECTION)), dec_clowds_1, SLOT(move(MOVE_DIRECTION)));
    connect(shuttle, SIGNAL(signal_fly(MOVE_DIRECTION)), dec_clowds_1_cover, SLOT(move(MOVE_DIRECTION)));

    connect(shuttle, SIGNAL(signal_fly_finish()), dec_clowds_1, SLOT(move_finish()));
    connect(shuttle, SIGNAL(signal_fly_finish()), dec_clowds_1_cover, SLOT(move_finish()));

//clowds 2
    connect(shuttle, SIGNAL(signal_fly(MOVE_DIRECTION)), dec_clowds_2, SLOT(move(MOVE_DIRECTION)));
    connect(shuttle, SIGNAL(signal_fly(MOVE_DIRECTION)), dec_clowds_2_cover, SLOT(move(MOVE_DIRECTION)));

    connect(shuttle, SIGNAL(signal_fly_finish()), dec_clowds_2, SLOT(move_finish()));
    connect(shuttle, SIGNAL(signal_fly_finish()), dec_clowds_2_cover, SLOT(move_finish()));

//clowds 3
    connect(shuttle, SIGNAL(signal_fly(MOVE_DIRECTION)), dec_clowds_3, SLOT(move(MOVE_DIRECTION)));
    connect(shuttle, SIGNAL(signal_fly(MOVE_DIRECTION)), dec_clowds_3_cover, SLOT(move(MOVE_DIRECTION)));

    connect(shuttle, SIGNAL(signal_fly_finish()), dec_clowds_3, SLOT(move_finish()));
    connect(shuttle, SIGNAL(signal_fly_finish()), dec_clowds_3_cover, SLOT(move_finish()));

//background
    connect(shuttle, SIGNAL(signal_fly(MOVE_DIRECTION)), bg_tile, SLOT(move(MOVE_DIRECTION)));
    connect(shuttle, SIGNAL(signal_fly(MOVE_DIRECTION)), bg_tile_cover, SLOT(move(MOVE_DIRECTION)));

    connect(shuttle, SIGNAL(signal_fly_finish()), bg_tile, SLOT(move_finish()));
    connect(shuttle, SIGNAL(signal_fly_finish()), bg_tile_cover, SLOT(move_finish()));

//shuttle fire
    connect(shuttle, SIGNAL(signal_fire_start(WEAPON_TYPE)), this, SLOT(slot_shuttle_fire(WEAPON_TYPE)));
    connect(shuttle, SIGNAL(signal_fire_finish(WEAPON_TYPE)), this, SLOT(slot_shuttle_fire_finish(WEAPON_TYPE)));

//menu
    connect(menu, SIGNAL(signal_menu_closed()), this, SLOT(slot_menu_visible()));
    connect(menu, SIGNAL(signal_menu_item_pressed(MENU_ITEM_TYPE)), this, SLOT(menu_item_pressed(MENU_ITEM_TYPE)));

    connect(shuttle,SIGNAL(Uuuups_I_die()),this,SLOT(Shuttle_die()));

//timer
    id_timer=startTimer(1000);
}

void GENERAL::timerEvent(QTimerEvent *)
{

    if(shuttle_life)
        shuttle_pos = QPoint(shuttle->pos().x() + (shuttle->boundingRect().width()/2), shuttle->pos().y() + (shuttle->boundingRect().height()/2));
    else
        shuttle_pos = QPoint();

}

GENERAL::~GENERAL()
{

    killTimer(id_timer);

}

void GENERAL::Add_Bullet_3(QPoint b)
{

    BULLET_3 *bullet_3 = new BULLET_3(b,scene,global_tl,cash);
    scene->addItem(bullet_3);

    connect(bullet_3,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

}

void GENERAL::Add_Oskolok()
{

    OSKOLOK *oskolok = new OSKOLOK(1+qrand()%(6-1+1), 1000, scene, global_tl,cash);
    scene->addItem(oskolok);

}

void GENERAL::Enemy_1_Now()
{

    ENEMY_1 *enemy_1 = new ENEMY_1(scene,global_tl,cash);
    scene->addItem(enemy_1);
    connect(enemy_1,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));
    connect(enemy_1,SIGNAL(Fire(QPoint)),SLOT(Add_Rocket_1(QPoint)));

}

void GENERAL::Enemy_2_Now()
{

    ENEMY_2 *enemy_2 = new ENEMY_2(scene, global_tl,cash);
    scene->addItem(enemy_2);
    connect(enemy_2,SIGNAL(Fire(QPoint,bool)),SLOT(Add_Rocket_2(QPoint,bool)));
    connect(enemy_2,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

}

void GENERAL::Enemy_3_Now()
{

    ENEMY_3 *enemy_3 = new ENEMY_3(scene,global_tl,cash);
    scene->addItem(enemy_3);

    LASER *l_1 = new LASER(scene, global_tl,cash);
    scene->addItem(l_1);

    connect(enemy_3,SIGNAL(Fire(QPoint)),l_1,SLOT(I_Am_Move(QPoint)));
    connect(enemy_3,SIGNAL(Kill_My_Laser()),l_1,SLOT(deleteLater()));

}

void GENERAL::Enemy_4_Now()
{

    ENEMY_4 *enemy_4 = new ENEMY_4(scene,global_tl,cash);
    scene->addItem(enemy_4);
    connect(enemy_4,SIGNAL(Fire(QPoint)),SLOT(Add_Bullet(QPoint)));
    connect(enemy_4,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

}

void GENERAL::Enemy_5_Now()
{

    QTimer::singleShot(1000, this, SLOT(Add_Enemy_5()) );
    QTimer::singleShot(3000, this, SLOT(Add_Enemy_5()) );
    QTimer::singleShot(5000, this, SLOT(Add_Enemy_5()) );

}

void GENERAL::Enemy_6_Now()
{

    ENEMY_6 *enemy_6 = new ENEMY_6(scene,global_tl,cash);
    scene->addItem(enemy_6);
    connect(enemy_6,SIGNAL(Fire(bool,int,bool,QPoint)),SLOT(Add_Bullet_2(bool,int,bool,QPoint)));
    connect(enemy_6,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

}

void GENERAL::Enemy_7_Now()
{

    ENEMY_7 *enemy_7 = new ENEMY_7(scene,global_tl,cash);
    scene->addItem(enemy_7);
    connect(enemy_7,SIGNAL(Shadow_Now()),SLOT(Add_Shadow_Enemy_7()));
    connect(enemy_7,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

}

void GENERAL::Enemy_8_Now()
{

    ENEMY_8 *enemy_8 = new ENEMY_8(scene,global_tl,cash);
    scene->addItem(enemy_8);
    connect(enemy_8,SIGNAL(Fire(QPoint,bool)),SLOT(Add_Spiral_1(QPoint,bool)));
    connect(enemy_8,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

}

void GENERAL::Enemy_9_Now()
{

    int line=10+qrand()%(150-10+1); // отсуп от экрана

    ENEMY_9 *enemy_9_1 = new ENEMY_9( QPoint(-200,G_H/2), QPoint((G_W/2)-50,line), -50, scene, global_tl,cash); // начало, конец, отклонение
    scene->addItem(enemy_9_1);

    ENEMY_9 *enemy_9_2 = new ENEMY_9( QPoint(-200,-200), QPoint((G_W/2)-150,line), -150, scene, global_tl,cash);
    scene->addItem(enemy_9_2);

    ENEMY_9 *enemy_9_3 = new ENEMY_9( QPoint(G_W/2,-200), QPoint((G_W/2)-250,line), -250, scene, global_tl,cash);
    scene->addItem(enemy_9_3);

    ENEMY_9 *enemy_9_4 = new ENEMY_9( QPoint(G_W+200,-200), QPoint((G_W/2)+50,line), 50, scene, global_tl,cash);
    scene->addItem(enemy_9_4);

    ENEMY_9 *enemy_9_5 = new ENEMY_9( QPoint(G_W+200,G_H/2), QPoint((G_W/2)+150,line), 150, scene, global_tl,cash);
    scene->addItem(enemy_9_5);

    connect(enemy_9_1,SIGNAL(Fire(QPoint)),SLOT(Add_Bullet_3(QPoint)));
    connect(enemy_9_2,SIGNAL(Fire(QPoint)),SLOT(Add_Bullet_3(QPoint)));
    connect(enemy_9_3,SIGNAL(Fire(QPoint)),SLOT(Add_Bullet_3(QPoint)));
    connect(enemy_9_4,SIGNAL(Fire(QPoint)),SLOT(Add_Bullet_3(QPoint)));
    connect(enemy_9_5,SIGNAL(Fire(QPoint)),SLOT(Add_Bullet_3(QPoint)));

    connect(enemy_9_1,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));
    connect(enemy_9_2,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));
    connect(enemy_9_3,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));
    connect(enemy_9_4,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));
    connect(enemy_9_5,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

}

void GENERAL::Enemy_10_Now()
{

    ENEMY_10_WARNING *enemy_10_warning = new ENEMY_10_WARNING(cash);
    scene->addItem(enemy_10_warning);
    connect(enemy_10_warning,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

    QPoint *zero_hour_point = new QPoint( 300+qrand()%((G_W-300)-300+1),G_H);           // Общая точка для некоторых обьектов

    ENEMY_SIGN *sign = new ENEMY_SIGN(QPoint(zero_hour_point->x(),zero_hour_point->y()+65), cash);
    scene->addItem(sign);
    connect(sign,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

    ENEMY_10_NORMAL *enemy_10_normal = new ENEMY_10_NORMAL(*zero_hour_point,scene,global_tl,cash);
    scene->addItem(enemy_10_normal);
    connect(enemy_10_normal,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

    ROCKET_3 *rocket_3 = new ROCKET_3(*zero_hour_point,scene,global_tl,cash);
    scene->addItem(rocket_3);

    QTimer::singleShot(16100, this, SLOT(Add_Oskolok()));
    QTimer::singleShot(17200, this, SLOT(Add_Oskolok()));
    QTimer::singleShot(18300, this, SLOT(Add_Oskolok()));
    QTimer::singleShot(19400, this, SLOT(Add_Oskolok()));
    QTimer::singleShot(20500, this, SLOT(Add_Oskolok()));

}

void GENERAL::Add_Spiral_1(QPoint r, bool left_orientation)
{

    SPIRAL_1 *spiral_1 = new SPIRAL_1(r,left_orientation,scene,global_tl,cash);
    scene->addItem(spiral_1);

}

void GENERAL::Add_Shadow_Enemy_7()
{

    SHADOW *shadow_1 = new SHADOW(scene,global_tl,cash);
    scene->addItem(shadow_1);
    connect(shadow_1,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

    QTimer::singleShot(3000,shadow_1,SLOT(Start()));

    ENEMY_SIGN *enemy_sign_1 = new ENEMY_SIGN(QPoint(shadow_1->pos().x()+80,shadow_1->pos().y()+40),cash);
    scene->addItem(enemy_sign_1);

}

void GENERAL::Add_Enemy_5()
{

    ENEMY_5 *enemy_5 = new ENEMY_5(shuttle_pos,scene,global_tl,cash);
    scene->addItem(enemy_5);
    connect(enemy_5,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

}

void GENERAL::Add_Rocket_1(QPoint r)
{

    Rocket_1 *r_1 = new Rocket_1(r,scene,global_tl,cash);
    scene->addItem(r_1);

}

void GENERAL::Add_Rocket_2(QPoint r, bool left_orientation)
{

    ROCKET_2 *r_2 = new ROCKET_2(r,left_orientation,scene,global_tl,cash);
    scene->addItem(r_2);

}

void GENERAL::Add_Bullet(QPoint begin)
{

    BULLET_1 *b_1 = new BULLET_1(begin,scene,global_tl,cash);
    scene->addItem(b_1);

    connect(b_1,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

}

void GENERAL::Add_Bullet_2(bool razlet_left, int angle_attacks,bool stvol_left,QPoint my_coords)
{

    BULLET_2 *bullet_2 = new BULLET_2(razlet_left,angle_attacks,stvol_left,my_coords,scene,global_tl,cash);
    scene->addItem(bullet_2);

    connect(bullet_2,SIGNAL(Kill_Me_Please(QGraphicsObject*)),SLOT(Delete_Controller(QGraphicsObject*)));

}

//слот срабатывает при нажатии клавиш
void GENERAL::slot_keys_pressed()
{
    QSet<int> *set = shuttle->get_set_keys();

    foreach (int key, *set)
        switch (key) {
            case Qt::Key_Escape:
                slot_menu_visible();
            break;
        }
}

void GENERAL::slot_menu_visible()
{
    if(menu->is_unvis()){
        //tl_anim_tiles->stop();
        hud->hide();
        menu->show();
        scene->setFocusItem(menu);

    }else{
        //tl_anim_tiles->start();
        hud->show();
        menu->hide();
        scene->setFocusItem(shuttle);

    }
}

void GENERAL::slot_shuttle_fire(WEAPON_TYPE type)
{

    if(shuttle_life){ // пока жив шатл

        switch (type) {
        case GATLING:
        {
            connect(tl_shuttle_gatling_spawn, SIGNAL(finished()), this, SLOT(slot_gatling_spawn()));
            slot_gatling_spawn();

            break;
        }

        case CANNON:
        {
            if(!shuttle_cannon_activated){
                connect(tl_shuttle_cannon_spawn, SIGNAL(finished()), this, SLOT(slot_cannon_spawn()));
                slot_cannon_spawn();
            }

            break;
        }

        default:
            break;
        }

    }

}

void GENERAL::slot_shuttle_fire_finish(WEAPON_TYPE type)
{
    switch (type) {
    case GATLING:
    {

        disconnect(tl_shuttle_gatling_spawn, SIGNAL(finished()), this, SLOT(slot_gatling_spawn()));
        tl_shuttle_gatling_spawn->stop();

        break;
    }

    default:
        break;
    }
}

void GENERAL::slot_gatling_spawn()
{

    if(!shuttle_life) return;

    GATLING_BULLET *bullet = new GATLING_BULLET(QPointF(shuttle->pos().x() + 73, shuttle->pos().y() + 50), global_tl, scene,cash);
    scene->addItem(bullet);

    bullet->setZValue(0.5);
    connect(tl_anim_tiles, SIGNAL(valueChanged(qreal)), bullet, SLOT(slot_run()));

    bullet = new GATLING_BULLET(QPointF(shuttle->pos().x() + 117, shuttle->pos().y() + 50), global_tl,scene,cash);
    scene->addItem(bullet);

    bullet->setZValue(0.5);
    connect(tl_anim_tiles, SIGNAL(valueChanged(qreal)), bullet, SLOT(slot_run()));

    tl_shuttle_gatling_spawn->start();
}

void GENERAL::slot_cannon_spawn()
{
    if(!shuttle_cannon_activated && shuttle->Get_Cannons_State()){
        hud->slot_cannon_fire();

        CANNON_BULLET *bullet = new CANNON_BULLET(QPointF(shuttle->pos().x() + 45, shuttle->pos().y() + 75), global_tl, scene,cash);
        scene->addItem(bullet);

        bullet->setZValue(10);
        connect(tl_anim_tiles, SIGNAL(valueChanged(qreal)), bullet, SLOT(slot_run()));

        bullet = new CANNON_BULLET(QPointF(shuttle->pos().x() + 128, shuttle->pos().y() + 75), global_tl, scene,cash);
        scene->addItem(bullet);

        bullet->setZValue(10);
        connect(tl_anim_tiles, SIGNAL(valueChanged(qreal)), bullet, SLOT(slot_run()));

        tl_shuttle_cannon_spawn->start();
        shuttle_cannon_activated = true;
    }else{
        slot_cannon_stop();
    }
}

void GENERAL::slot_cannon_stop()
{
    disconnect(tl_shuttle_cannon_spawn, SIGNAL(finished()), this, SLOT(slot_cannon_spawn()));
    tl_shuttle_cannon_spawn->stop();
    shuttle_cannon_activated = false;
}


void GENERAL::LvL_1_Start()
{

    QTimer::singleShot(1000, shuttle, SLOT(start_mission_anim()));

    QTimer::singleShot(50, hud, SLOT(slot_visible_hud()));
    QTimer::singleShot(5000, hud, SLOT(show_hud()));

    QTimer::singleShot(1000, sound, SLOT(Play_Now_Slot()) );

    QTimer::singleShot(3000, stage_text, SLOT(anim_show()));
    QTimer::singleShot(4000, stage_text, SLOT(anim_hide()));

//    QTimer::singleShot(5000, this, SLOT(Enemy_4_Now()) );
//    QTimer::singleShot(6000, this, SLOT(Enemy_4_Now()) );
//    QTimer::singleShot(7000, this, SLOT(Enemy_4_Now()) );
//    QTimer::singleShot(8000, this, SLOT(Enemy_4_Now()) );

//    QTimer::singleShot(5000, this, SLOT(Enemy_1_Now()) );       // Волна 1 начинаеться с 5000
//    QTimer::singleShot(8000, this, SLOT(Enemy_1_Now()) );
//    QTimer::singleShot(10000, this, SLOT(Enemy_1_Now()) );
//    QTimer::singleShot(12000, this, SLOT(Enemy_1_Now()) );
//    QTimer::singleShot(14000, this, SLOT(Enemy_1_Now()) );
//    QTimer::singleShot(16000, this, SLOT(Enemy_1_Now()) );
//    QTimer::singleShot(18000, this, SLOT(Enemy_1_Now()) );
//    QTimer::singleShot(20000, this, SLOT(Enemy_1_Now()) );
//    QTimer::singleShot(22000, this, SLOT(Enemy_1_Now()) );

//    QTimer::singleShot(26000, this, SLOT(Enemy_4_Now()) );
//    QTimer::singleShot(27000, this, SLOT(Enemy_4_Now()) );
//    QTimer::singleShot(28000, this, SLOT(Enemy_4_Now()) );
//    QTimer::singleShot(29000, this, SLOT(Enemy_4_Now()) );

//    QTimer::singleShot(27000, this, SLOT(Enemy_3_Now()) );

//    QTimer::singleShot(45000, this, SLOT(Enemy_1_Now()) );
//    QTimer::singleShot(47500, this, SLOT(Enemy_2_Now()) );
//    QTimer::singleShot(51000, this, SLOT(Enemy_1_Now()) );
//    QTimer::singleShot(54000, this, SLOT(Enemy_5_Now()) );
//    QTimer::singleShot(57000, this, SLOT(Enemy_5_Now()) );

    QTimer::singleShot(5000, this, SLOT(Enemy_1_Now()) );                 // Волна 3 начинаеться с 134000
    QTimer::singleShot(7000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(10000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(11000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(13000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(16000, this, SLOT(Enemy_6_Now()) );

    QTimer::singleShot(20000, this, SLOT(Enemy_7_Now()) );
    QTimer::singleShot(25000, this, SLOT(Enemy_7_Now()) );
    QTimer::singleShot(35000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(38000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(41000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(42000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(43000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(44000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(43000, this, SLOT(Enemy_3_Now()) );
    QTimer::singleShot(45000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(50000, this, SLOT(Enemy_5_Now()) );

    QTimer::singleShot(51000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(51000, this, SLOT(Enemy_3_Now()) );
    QTimer::singleShot(52000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(54000, this, SLOT(Enemy_5_Now()) );

    QTimer::singleShot(56000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(57000, this, SLOT(Enemy_3_Now()) );
    QTimer::singleShot(58000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(60000, this, SLOT(Enemy_5_Now()) );

    QTimer::singleShot(60000, this, SLOT(Enemy_7_Now()) );
    QTimer::singleShot(62000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(63000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(64000, this, SLOT(Enemy_6_Now()) );

    QTimer::singleShot(1000+67000, stage_text, SLOT(anim_show()));
    QTimer::singleShot(5000+67000, stage_text, SLOT(anim_hide()));


    QTimer::singleShot(30000, this, SLOT(slot_bg_mission_to_water()));

    QTimer::singleShot(5000+67000, this, SLOT(Enemy_4_Now()) );       // Волна 2 начинаеться с 67000
    QTimer::singleShot(6000+67000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(7000+67000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(7000+67000, this, SLOT(Enemy_5_Now()) );
    QTimer::singleShot(15000+67000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(18000+67000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(16000+67000, this, SLOT(Enemy_3_Now()) );

    QTimer::singleShot(33000+67000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(35000+67000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(37000+67000, this, SLOT(Enemy_6_Now()) );

    QTimer::singleShot(40000+67000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(42000+67000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(44000+67000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(46000+67000, this, SLOT(Enemy_2_Now()) );
    QTimer::singleShot(50000+67000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(53000+67000, this, SLOT(Enemy_2_Now()) );
    QTimer::singleShot(57000+67000, this, SLOT(Enemy_2_Now()) );


    QTimer::singleShot(57000+67000, this, SLOT(Enemy_9_Now()) );
    QTimer::singleShot(65000+67000, this, SLOT(Enemy_9_Now()) );

    QTimer::singleShot(1000+134000, stage_text, SLOT(anim_show()));
    QTimer::singleShot(5000+134000, stage_text, SLOT(anim_hide()));

    QTimer::singleShot(5000+134000, this, SLOT(Enemy_1_Now()) );                 // Волна 3 начинаеться с 134000
    QTimer::singleShot(7000+134000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(10000+134000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(11000+134000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(13000+134000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(16000+134000, this, SLOT(Enemy_6_Now()) );

    QTimer::singleShot(20000+134000, this, SLOT(Enemy_7_Now()) );
    QTimer::singleShot(25000+134000, this, SLOT(Enemy_7_Now()) );
    QTimer::singleShot(35000+134000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(38000+134000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(41000+134000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(42000+134000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(43000+134000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(44000+134000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(43000+134000, this, SLOT(Enemy_3_Now()) );
    QTimer::singleShot(45000+134000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(50000+134000, this, SLOT(Enemy_5_Now()) );

    QTimer::singleShot(1000+194000, stage_text, SLOT(anim_show()));
    QTimer::singleShot(5000+194000, stage_text, SLOT(anim_hide()));

    QTimer::singleShot(5000+194000, this, SLOT(Enemy_9_Now()) );                   // Волна 4 начинаеться с 194000
    QTimer::singleShot(25000+194000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(28000+194000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(31000+194000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(35000+194000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(36000+194000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(37000+194000, this, SLOT(Enemy_8_Now()) );
    QTimer::singleShot(40000+194000, this, SLOT(Enemy_8_Now()) );
    QTimer::singleShot(43000+194000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(46000+194000, this, SLOT(Enemy_7_Now()) );
    QTimer::singleShot(50000+194000, this, SLOT(Enemy_9_Now()) );
    QTimer::singleShot(51000+194000, this, SLOT(Enemy_7_Now()) );

    QTimer::singleShot(1000+265000, stage_text, SLOT(anim_show()));
    QTimer::singleShot(5000+265000, stage_text, SLOT(anim_hide()));

    QTimer::singleShot(5000+265000, this, SLOT(Enemy_5_Now()) );          // Волна 5 начинаеться с 265000
    QTimer::singleShot(8000+265000, this, SLOT(Enemy_5_Now()) );
    QTimer::singleShot(11000+265000, this, SLOT(Enemy_2_Now()) );
    QTimer::singleShot(13000+265000, this, SLOT(Enemy_1_Now()) );
    QTimer::singleShot(15000+265000, this, SLOT(Enemy_2_Now()) );
    QTimer::singleShot(18000+265000, this, SLOT(Enemy_5_Now()) );

    QTimer::singleShot(27000+265000, this, SLOT(Enemy_3_Now()) );
    QTimer::singleShot(29000+265000, this, SLOT(Enemy_8_Now()) );
    QTimer::singleShot(33000+265000, this, SLOT(Enemy_8_Now()) );
    QTimer::singleShot(35000+265000, this, SLOT(Enemy_8_Now()) );
    QTimer::singleShot(38000+265000, this, SLOT(Enemy_8_Now()) );

    QTimer::singleShot(40000+265000, this, SLOT(Enemy_9_Now()) );

    QTimer::singleShot(60000+265000, this, SLOT(Enemy_10_Now()) );

    QTimer::singleShot(77000+265000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(78000+265000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(79000+265000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(80000+265000, this, SLOT(Enemy_4_Now()) );

    QTimer::singleShot(80000+265000, this, SLOT(Enemy_3_Now()) );
    QTimer::singleShot(87000+265000, this, SLOT(Enemy_6_Now()) );
    QTimer::singleShot(90000+265000, this, SLOT(Enemy_6_Now()) );

    QTimer::singleShot(94000+265000, this, SLOT(Enemy_7_Now()) );
    QTimer::singleShot(96000+265000, this, SLOT(Enemy_7_Now()) );

    QTimer::singleShot(96000+265000, this, SLOT(Enemy_10_Now()) );
    QTimer::singleShot(98000+265000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(100000+265000, this, SLOT(Enemy_4_Now()) );

    QTimer::singleShot(112000+265000, this, SLOT(Enemy_3_Now()) );
    QTimer::singleShot(113000+265000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(114000+265000, this, SLOT(Enemy_4_Now()) );
    QTimer::singleShot(115000+265000, this, SLOT(Enemy_4_Now()) );

    QTimer::singleShot(120000+265000, to_be_continued_text, SLOT(anim_show()));

}

void GENERAL::menu_item_pressed(MENU_ITEM_TYPE type)
{
    switch (type) {
    case STORY:
    case BRUTAL:
        slot_menu_visible();
        LvL_1_Start();
        break;


    case QUIT:
        QApplication::closeAllWindows();
        break;

    default:
        break;
    }
}

void GENERAL::Shuttle_die()
{
    shuttle_life=false;
}

void GENERAL::Delete_Controller(QGraphicsObject *temp)
{
    scene->removeItem(temp);
}

void GENERAL::load_num_explosions()
{
    vect_exp_count = new QVector<int>();

    for(int i=0;i<12;i++){
        QDir dir("Res/explosions/png/" + QString::number(i));
        QStringList tmp = dir.entryList();
        tmp.removeOne("..");
        tmp.removeOne(".");

        vect_exp_count->append(tmp.count());
    }
}

void GENERAL::slot_bg_mission_to_water()
{
    connect(bg_tile_cover, SIGNAL(signal_invisible()), bg_tile_cover, SLOT(slot_blend_to_water()));
}

void GENERAL::slot_bg_mission_to_city()
{
    connect(bg_tile_cover, SIGNAL(signal_invisible()), bg_tile_cover, SLOT(slot_blend_to_city()));
}
